// pages/goodsList/goodsList.js
import {
    request
} from "../../request/request.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentIndex: 0,
        goodsList: [],
        sortGoodsList: [], //价格
        sellGoodsList: [],
        tabs: [{
                id: 0,
                value: "综合",
                isActive: true
            },
            {
                id: 1,
                value: "销量",
                isActive: false
            },
            {
                id: 2,
                value: "价格",
                isActive: false
            }
        ]
    },
    Params: {
        query: "",
        cid: "",
        pagenum: 1,
        pagesize: 10
    },
    totalPages: 1,
    // 获取商品列表的数据
    async getGoodsList() {
        wx.showLoading({
            title: '努力加载中',
        })
        const res = await request({
            url: "https://api-hmugo-web.itheima.net/api/public/v1/goods/search",
            data: this.Params
        })
        const total = res.data.message.total
        // 计算总页数
        this.totalPages = Math.ceil(total/this.Params.pagesize)
        this.setData({
            goodsList: [...this.data.goodsList, ...res.data.message.goods]
        })
        //关闭下拉刷新窗口
        wx.stopPullDownRefresh();
    },
    sortList(list) {
        //商品按照价格排序
        list.sort(function (a, b) {
            if (a.goods_price > b.goods_price) {
                return 1
            }
            if (a.goods_price < b.goods_price) {
                return -1
            }
            return 0
        })
        return list
    },
    sortListNumber(list) {
        //商品按照价格排序
        list.sort(function (a, b) {
            if (a.goods_id > b.goods_id) {
                return 1
            }
            if (a.goods_id < b.goods_id) {
                return -1
            }
            return 0
        })
        return list
    },
    // 标题点击事件 从子组件传递过来
    async hi(e) {
        this.data.currentIndex = e.detail.index
        const {
            index
        } = e.detail
        let {
            tabs
        } = this.data
        tabs.forEach((a, i) => i === index ? a.isActive = true : a.isActive = false)
        this.setData({
            tabs
        })
        // 价格
        if (this.data.currentIndex == 2) {
            const res = await request({
                url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search',
                data: this.Params
            })
            const newlist = this.sortList(res.data.message.goods)
            this.setData({
                sortGoodsList:newlist,
            })
        }
        // 销量
        if (this.data.currentIndex == 1) {
            const res = await request({
                url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search',
                data: this.Params
            })
            const newlist = this.sortListNumber(res.data.message.goods)
            this.setData({
                sellGoodsList:newlist
                // goodsList: 
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.Params.cid = options.cid
        this.getGoodsList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.setData({
            goodsList: [],
            // sortGoodsList:[],
            // sellGoodsList:[]
        })
        // 重置页码
        this.Params.pagenum == 1
        this.getGoodsList()

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.Params.pagenum >= this.totalPages) {
            wx.showToast({
                title: '已经到底了',
            })
        } else {
            this.Params.pagenum++
            this.getGoodsList()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})